package com.blog.controller;

import com.blog.Utils.Result;
import com.blog.annotion.LoginAnnotatio;
import com.blog.base.ThreadLocalUserId;
import com.blog.service.LikeService;
import com.blog.bean.vo.LikeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
@RequestMapping("/like")
@Slf4j
public class LikeController {

    @Autowired
    private LikeService likeService;

    /**
     * 对帖子的点赞
     * @param postId
     * @param index
     * @param session
     * @return
     */
    @LoginAnnotatio //进行没有登录点击进行拦截
    @PutMapping
    public Result put(@RequestParam("id") String postId,@RequestParam("index") Integer index, HttpSession session){
        //得到当前的对象id.
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        String userID = userId.toString();
        LikeVO likeVO = likeService.like(postId,userID,index);
        System.out.println(likeVO);

        return Result.success(likeVO);
    }

    /**
     * 这是对评论点赞的处理方法。
     * @param postId
     * @param index
     * @param session
     * @return
     */
    @PutMapping("/comment")
    @LoginAnnotatio
    public Result commentLike(@RequestParam("id") String postId,@RequestParam("index") Integer index, HttpSession session){
        //得到当前的对象id.
        log.info("进入了评论点赞方法");
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        String userID = userId.toString();
        LikeVO likeVO = likeService.commentlike(postId,userID,index);

        System.out.println(likeVO);
        return Result.success(likeVO);
    }

    /**
     * 这是对评论回复的点赞
     * @param postId
     * @param index
     * @param index2
     * @param session
     * @return
     */
    @PutMapping("/commentReply")
    @LoginAnnotatio
    public Result commentReplyLike(@RequestParam("id") String postId,@RequestParam("index") Integer index,@RequestParam("index2") Integer index2, HttpSession session){
        //得到当前的对象id.
        log.info("进入了评论回复点赞方法");
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        String userID = userId.toString();
        LikeVO likeVO = likeService.commentReplylike(postId,userID,index,index2);

        System.out.println(likeVO);
        return Result.success(likeVO);
    }
}
